package view.maze;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import view.game2048.Game2048Tile;

public class MazeBoard extends Composite 
{
	// Data Members //
	protected int[][] boardData;
	private int N,M;

	// Board CTOR //
	public  MazeBoard(Composite parent, int style) 
	{
		// Composite Parent CTOR
		super(parent, style);

		// Data Members Initialization
		this.N = 20;
		this.M = 20;
		this.boardData = new int[N][M];

		// Setting the boards's layout and background color
		setLayout(new GridLayout(N, false));
		setBackground(new Color(Display.getCurrent(), 0, 0, 0));

		// Initialize the Tiles array
		final MazeTile tiles[][] = new MazeTile[N][M];
		for(int i = 0; i < N; i++)
			for (int j = 0; j < M; j++)
			{
				
				tiles[i][j] = new MazeTile(this, SWT.FILL);
				tiles[i][j].setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
				tiles[i][j].setValue(boardData[i][j]);
			}

		// Add a paintListener that would redraw all of the Tiles in the tiles array
		addPaintListener(new PaintListener() 
		{	
			@Override
			public void paintControl(PaintEvent e) 
			{
				for(int i = 0; i < N; i++)
					for (int j = 0; j < M; j++)
						tiles[i][j].setValue(boardData[i][j]);
			}
		});
	}

	// Set the boardData array to the given array
	public void setBoardData(int[][] newBoardData)
	{
		this.boardData = newBoardData;
	}
}
